/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package k3tool.cli;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import k3tool.opf.model.KindleMetaData;
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;
import nu.xom.ParsingException;
import nu.xom.Serializer;
import nu.xom.ValidityException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Jeff Huang
 */
public class KindleMetaDataConfigHelper {

    public static final String[] INTERESTED_FIELDS = {
        "title", "language", "creator", "publisher", "subject",
        "desc", "coverPath", "welcomePath", "tocFile", "inputCharset"
    };

    public static void createConfigFile(KindleMetaData metaData, File file) throws FileNotFoundException, IOException {
        Element root = new Element("Book");

        //title
        for (String fieldName : INTERESTED_FIELDS) {
            Element item = new Element("Info");
            item.addAttribute(new Attribute("name", fieldName));
            try {
                String value = (String) PropertyUtils.getSimpleProperty(metaData, fieldName);
                if(value == null)
                    value = "";
                item.appendChild(value);
            }catch(IllegalAccessException ex){
                throw new RuntimeException(ex);
            }catch(InvocationTargetException ex){
                throw new RuntimeException(ex);
            }catch(NoSuchMethodException ex){
                throw new RuntimeException(ex);
            }
            root.appendChild(item);
        }
        Document doc = new Document(root);
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(file);
            Serializer s = new Serializer(fos);
            s.setIndent(4);
            s.write(doc);
            fos.close();
            fos = null;
        }finally{
            if(fos != null){
                try{
                    fos.close();
                }catch(IOException ex){
                    
                }
            }
        }
        
    }

    public static KindleMetaData loadMetaDataFromConfigFile(File file) throws IOException {
        Builder builder = new Builder();
        Document doc = null;
        try{
            doc = builder.build(file);
        }catch(ParsingException ex){
            throw new RuntimeException(ex);
        }
        KindleMetaData metaData = new KindleMetaData();
        for(String fieldName : INTERESTED_FIELDS){
            String xpath = String.format("/Book/Info[@name = '%s']/text()", fieldName);
            Nodes nodes = doc.query(xpath);
            String value = null;
            if(nodes.size() != 0){
                value = nodes.get(0).getValue();
                if(StringUtils.isEmpty(value)){
                    value = null;
                }
            }
            try{
                PropertyUtils.setSimpleProperty(metaData, fieldName, value);
            }catch(IllegalAccessException ex){
                throw new RuntimeException(ex);
            }catch(InvocationTargetException ex){
                throw new RuntimeException(ex);
            }catch(NoSuchMethodException ex){
                throw new RuntimeException(ex);
            }
        }
        return metaData;
    }

}
